package leetcode.day_2021_03_8_2021_03_14;

/**
 * 50. Pow(x, n)
 * https://leetcode-cn.com/problems/powx-n/
 */
public class LeetCode0050 {

    public static double myPow(double x, int n) {
        if (x == 0f) return 0d;
        if (n == 0) return 1.0d;
        long b = n;
        if (b < 0) {
            x = 1/x;
            b = -b;
        }
        double res = 1.0;
        while (b > 0) {
            if (b%2 == 1) {
                res = res * x;
            }
            x *= x;
            b = b/2;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(myPow(2.00000, 10));
    }
}
